const path = require('path')
import pkg from './package.json'
import { terser } from "rollup-plugin-terser";
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';

const resolve = (dir) => {
  return path.join(__dirname, dir)
}
// 输出路径
const outPath = resolve('dist')
// 最小化版本文件头注释信息
const banner =
  `/*!
 * ${pkg.name} v${pkg.version}
 * (c) 2020-2021 ${pkg.author}
 * Released under the ${pkg.license} License.
 */
`;

// 输出
const out = [
  {
    file: outPath + '/jcore_cjs.js',
    format: 'cjs'
  },
  {
    file: outPath + '/jcore_esm.js',
    format: 'esm'
  },
  {
    file: outPath + '/jcore_umd.js',
    format: 'umd',
    name: 'jcore'
  }
]
// 压缩文件打包配置
function getOutMini(out) {
  let miniOutList = JSON.parse(JSON.stringify(out))
  for (let item of miniOutList) {
    item.banner = banner
    item.plugins = [terser({ compress: { drop_console: true } })]
    item.file = item.file.slice(0, item.file.lastIndexOf('.js')) + '.min.js'
  }
  return miniOutList
}

export default {
  input: resolve('src/main.js'),
  output: [
    ...out,
    ...getOutMini(out)
  ],
  plugins: [
    nodeResolve(),
    commonjs()
  ]
}